using System;
using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;

/*	-----------------------------------------------------------------------	
	Copyright:	umlungu consulting (pty) ltd
	Author:		Alan Benington
	Started:	2007-10-27
	Status:		beta
	Version:	2.0.0
	Build:		20071027
	License:	GNU General Public License
	-----------------------------------------------------------------------	*/

/*	-----------------------------------------------------------------------	
	Development Notes:
	==================
	20071027:	Start of .Net2 version from WinServiceX.0.0.1
	-----------------------------------------------------------------------	*/

namespace umlungu.engineX.winserviceX {
	/// <summary>
	/// A base class for a Windows Service installer
	/// </summary>
	[RunInstaller(true)]
	public class x_installer : System.Configuration.Install.Installer {
		#region Invisible properties
		private ServiceProcessInstaller servprocInstaller;
		private ServiceInstaller servInstaller;
		#endregion

		#region Constructors/Destructors
		/// <overloads>Constructor</overloads>
		/// <summary>Default constructor. Note that the Initialize method is not run automatically.</summary>
		public x_installer() {
		}
		/// <summary>Runs the Initialize method upon construction</summary>
		/// <param name="ServiceName">Name of the Windows service to be installed</param>
		public x_installer(string ServiceName) {
			Initialize(ServiceName, false);
		}
		/// <summary>Runs the Initialize method upon construction</summary>
		/// <param name="ServiceName">Name of the Windows service to be installed</param>
		/// <param name="Windows2000">Flag set true if system is windows 2000</param>
		public x_installer(string ServiceName, bool Windows2000) {
			Initialize(ServiceName, Windows2000);
		}
		#endregion

		#region Public methods
		/// <overloads>Initialise the service</overloads>
		/// <summary>
		/// Default intialisation method.
		/// <para>The Process runs on the LocalService (or LocalSystem) account and start mode is set to 'Automatic'</para>
		/// </summary>
		/// <param name="ServiceName">Name of the Windows service to be installed</param>
		/// <param name="Windows2000">Flag set true if system is windows 2000</param>
		public void Initialize(string ServiceName, bool Windows2000) {
			// servprocInstaller
			this.servprocInstaller = new ServiceProcessInstaller();
			if (Windows2000)
				this.servprocInstaller.Account = ServiceAccount.LocalSystem;
			else
				this.servprocInstaller.Account = ServiceAccount.LocalService;
			this.servprocInstaller.Password = null;
			this.servprocInstaller.Username = null;
			// servInstaller
			this.servInstaller = new ServiceInstaller();
			this.servInstaller.ServiceName = ServiceName;
			this.servInstaller.StartType = ServiceStartMode.Automatic;
			// x_installer
			this.Installers.AddRange(new System.Configuration.Install.Installer[] { this.servprocInstaller, this.servInstaller } );
		}
		/// <summary>
		/// Default intialisation method.
		/// <para>The Process runs on a User account and start mode is set to 'Automatic'</para>
		/// </summary>
		/// <param name="ServiceName">Name of the Windows service to be installed</param>
		/// <param name="Username">user account under which to run the service</param>
		/// <param name="Password">password of the user account</param>
		public void Initialize(string ServiceName, string Username, string Password) {
			// servprocInstaller
			this.servprocInstaller = new ServiceProcessInstaller();
			this.servprocInstaller.Account = ServiceAccount.User;
			this.servprocInstaller.Password = Password;
			this.servprocInstaller.Username = Username;
			// servInstaller
			this.servInstaller = new ServiceInstaller();
			this.servInstaller.ServiceName = ServiceName;
			this.servInstaller.StartType = ServiceStartMode.Automatic;
			// x_installer
			this.Installers.AddRange(new System.Configuration.Install.Installer[] { this.servprocInstaller, this.servInstaller } );
		}
		#endregion
	}
}
